import type { PermissionItemType } from '@/types/api/permission'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

export const usePermissionStore = defineStore('permission', () => {
  const permission = ref<PermissionItemType[]>([])

  const setPermission = (payload: PermissionItemType[]) => {
    permission.value = payload
  }

  const currentPermission = ref<PermissionItemType | null>(null)
  const setCurrentPermission = (payload: PermissionItemType | null) => {
    currentPermission.value = payload
  }

  const buttonPermissions = computed(() => {
    if (!currentPermission.value || !currentPermission.value.children) return []
    return currentPermission.value.children
      .filter((item) => item.type === 'button')
      .map((item) => item.permissionCode)
  })

  return { permission, setPermission, currentPermission, setCurrentPermission, buttonPermissions }
})
